import { createRouter, createWebHashHistory } from 'vue-router'
import { useRoutes } from '../views/user/router'
import { homeRoutes } from '../views/Home/router'
import { findRoutes } from '../views/Find/router'
import { classRoutes } from '../views/Classify/router'
import { guideRoutes } from '../views/Guide/router'
import { errorRouter } from '../views/Error/router'
import { showConfirmDialog } from 'vant'
import { getweek } from '../Hooks/getweek'

const { thisweek } = getweek()


const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  ...errorRouter,
  ...guideRoutes,
  ...findRoutes,
  ...homeRoutes,
  ...useRoutes,
  ...classRoutes
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const usertoken = localStorage.getItem('userToken')
  const wek = thisweek(new Date())
  const thiswek = localStorage.getItem('week')
  if (to.fullPath == '/find' || to.fullPath == '/user') {
    if (usertoken) {
      next()
    } else {
      next()
      showConfirmDialog({
        message:
          '您还没有登录,去登录?',
      })
        .then(() => {
          router.push('/user/login')
        })
        .catch(() => {
        });
    }
  } else if (to.fullPath == '/home') {
    if (wek == thiswek) {
      next()
    } else {
      router.push('/guide')
    }
  } else {
    next()
  }
})

export default router
